package data;

import java.util.ArrayList;
import java.util.Iterator;

import data.model.Processor;

/**
 * Container for Processors, 
 * 
 * @author Will Home
 *
 */
public class ProcList {

	private static ArrayList<Processor> procList;
	
	public ProcList(){
		procList = new ArrayList<Processor>();
	}
	
	/**
	 * check whether an processor is in the list, if so return it.
	 * 
	 * @param name - Processor name
	 * @return Processor
	 */
	public static Processor getProc(String name)
	{
		Processor curr;
		Iterator<Processor> it = procList.iterator();
		
		while(it.hasNext())
		{
			curr = it.next();
			if(curr.getProcName().equals(name))
				return curr;
		}
		
		return null;
	}
	
	/**
	 * 
	 * @return array of current processors
	 */
	public static Object[] getList()
	{
		return procList.toArray();
	}
	
	
	/**
	 * check whether an Processor is in the list, if so return it.
	 * 
	 * @param name Processor ID
	 * @return Processor
	 */
	public static Processor getProc(int procID)
	{
		Processor curr;
		Iterator<Processor> it = procList.iterator();
		
		while(it.hasNext())
		{
			curr = it.next();
			if(curr.getProcID() == procID)
				return curr;
		}
		
		return null;
	}
	
	/**
	 * adds a new Processor to the list
	 * 
	 * @param name - Processor name
	 * @return success check
	 */
	public static boolean addProc(String name)
	{
		return procList.add(new Processor(name));
	}
	
	/**
	 * removes the given Processor from the list
	 * 
	 * @param procID
	 * @return success check
	 */
	public static boolean removeProc(int procID)
	{
		Iterator<Processor> it = procList.iterator();
		
		while(it.hasNext())
		{
			if(it.next().getProcID() == procID)
			{
				it.remove();
				return true;
			}
		}
		return false;
	}
	
	/**
	 * removes the given Processor from the list
	 * 
	 * @param name
	 * @return success check
	 */
	public static boolean removeProc(String name)
	{
		Iterator<Processor> it = procList.iterator();
		
		while(it.hasNext())
		{
			if(it.next().getProcName().equals(name))
			{
				it.remove();
				return true;
			}
		}
		return false;
	}
	
	/**
	 * remove all processors
	 */
	public static void clear()
	{
		procList.clear();
	}
	
}
